package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.dao.UserPeakDao;
import io.renren.entity.UserPeakEntity;
import io.renren.service.UserPeakService;
import io.renren.util.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


@Service("userPeakService")
public class UserPeakServiceImpl extends ServiceImpl<UserPeakDao, UserPeakEntity> implements UserPeakService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserPeakEntity> page = this.selectPage(
                new Query<UserPeakEntity>(params).getPage(),
                new EntityWrapper<UserPeakEntity>()
        );

        return new PageUtils(page);
    }



    @Override
    public boolean createNewPeak(UserPeakEntity UserPeakEntity) {
        return this.insert(UserPeakEntity);
    }

    @Override
    public double getTotalPriceByCID(int campaignId) {

        UserPeakEntity campaign = this.selectOne(new EntityWrapper<UserPeakEntity>().setSqlSelect("SUM(price) totalPrice").eq("campaign_id", campaignId));

        if(null == campaign) {
            return 0;
        }

        return campaign.getTotalPrice();
    }

    @Override
    public boolean checkBackerByUid(int campaignId, String userId) {


        List<UserPeakEntity> list = this.selectList(new EntityWrapper<UserPeakEntity>().eq("campaign_id", campaignId).eq("user_id", userId));

        if(CollectionUtils.isEmpty(list)) {
            return false;
        }else{
            return true;
        }

    }


}
